package main

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"

	"gitverse.ru/panarinv/csvchk"
)

func main() {
	sep := flag.String("d", "\t", "field delimiter")
	str := flag.String("s", "", "data structure")
	hdr := flag.Bool("header", false, "first line without validation")
	trm := flag.Bool("trim", false, "trimming values")
	flag.Parse()
	stdinstat, _ := os.Stdin.Stat()
	if flag.NFlag() == 0 && stdinstat.Mode()&os.ModeCharDevice > 0 {
		fmt.Fprintf(
			os.Stderr,
			"Validator of CSV data stream.\n"+
				"Usage: %v [OPTIONS] <input >output 2>errors\n"+
				"Options:\n",
			filepath.Base(os.Args[0]))
		flag.PrintDefaults()
		fmt.Fprint(
			os.Stderr,
			"Structure example: 'i2*;b;i4;f;n(3,2);d;c(10)'\n"+
				"  * - required field;\n"+
				"  (precision,scale) or (length).\n"+
				"Supported types:\n"+
				"  b, bool, boolean;\n"+
				"  i2, int2, i16, int16, smallint;\n"+
				"  i4, int4, i32, int32, i, int, integer;\n"+
				"  i8, int8, i64, int64, bigint;\n"+
				"  r, real, f4, float4, f32, float32;\n"+
				"  f, float, f8, float8, f64, float64, double;\n"+
				"  n, numeric, decimal (with precision and scale);\n"+
				"  d, date, t, timestamp, timestamptz;\n"+
				"  c, char, character, v, varchar, text (with length).\n"+
				"Report format:\n"+
				"  fld_index;\n"+
				"  fld_type;\n"+
				"  err_type;\n"+
				"  err_value;\n"+
				"  err_line;\n"+
				"  err_count.\n")
		os.Exit(3)
	}

	csvchk.Check(
		*str,
		[]rune(*sep)[0],
		*hdr,
		*trm,
		os.Stdin,
		os.Stdout,
		os.Stderr)

}
